package com.szgr.baoyin.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.szgr.baoyin.R;
import com.szgr.baoyin.model.HomePageImage;
import com.szgr.baoyin.utils.MyTool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 15/11/9.
 */
public class SwitchPictureAdapter extends PagerAdapter {
    private Context mContext;
    private List<ImageView> mImageViews;
    private List<HomePageImage> mImgUrls;
    private List<HomePageImage> mImgUrls_copy;
    private LinearLayout mPoints;
    private ViewPager mViewPage;
    private View.OnClickListener listener;

    public SwitchPictureAdapter(Context mContext, ViewPager page,
                                List<HomePageImage> mImgUrls, View view,View.OnClickListener listener) {
        super();
        this.mContext = mContext;
        this.mImgUrls = mImgUrls;
        this.mImageViews = new ArrayList<ImageView>();
        this.mViewPage = page;
        this.mPoints = (LinearLayout) view.findViewById(R.id.auto_indicator);
        mPoints.removeAllViews();
        mImgUrls_copy = new ArrayList<>();
        mImgUrls_copy.addAll(mImgUrls);
        if(mImgUrls.size() > 0) {
            initData(mImgUrls_copy);
            initViews(mImgUrls_copy);
        }
        this.mViewPage.setOnPageChangeListener(new PagerListener());
        this.listener = listener;
    }

    private void initData(List<HomePageImage> mImgUrls_copy) {
        // 实现无限循环，另外添加两张图片
        mImgUrls_copy.add(0, mImgUrls_copy.get(mImgUrls_copy.size() - 1)); // 第一个位置添加最后一张
        mImgUrls_copy.add(mImgUrls_copy.get(1));// 最后一个位置添加第0张
        // 添加点
        for (int i = 0; i < mImgUrls_copy.size() - 2; i++) {
            ImageView point = new ImageView(mContext);
            int dp = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 8, mContext.getResources()
                            .getDisplayMetrics());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    dp, dp);
            int margin = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, 4, mContext.getResources()
                            .getDisplayMetrics());
            params.setMargins(margin, 0, margin, 0);
            point.setLayoutParams(params);
            if (i == 0) {
                point.setBackgroundResource(R.drawable.judge_02);
            } else {
                point.setBackgroundResource(R.drawable.judge_01);
            }
            mPoints.addView(point);
        }
    }

    private void initViews(List<HomePageImage> mImgUrls_copy) {
        if (mImgUrls_copy != null) {
            for (int i = 0; i < mImgUrls_copy.size(); i++) {
                ImageView view = new ImageView(mContext);
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                view.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                MyTool.showPicture(mContext, view,mImgUrls_copy.get(i).getImage());
                mImageViews.add(view);
                if (mImgUrls_copy.size() <= 3) {// 说明只有一张图片
                    break;
                }
            }
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView view = null;
        if(mImageViews.size() > 0) {
            view = mImageViews.get(position);
            view.setOnClickListener(listener);
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeAllViews();
            }

            container.addView(view);
        }
        return view;
    }

    @Override
    public int getCount() {
        if (mImgUrls_copy == null) {
            return 0;
        }
        if (mImgUrls_copy.size() <= 3) {
            return 1;
        }
        return mImgUrls_copy.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {

        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ImageView) object);
    }

    public void setData(List<HomePageImage> data) {
        mImgUrls = data;
        notifyDataSetChanged();
    }

    private class PagerListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int pos) {
            if (mImageViews.size() > 1) { // 图片多余一张才轮播
                if (pos < 1) {
                    pos = mImageViews.size() - 2;
                    mViewPage.setCurrentItem(pos, false);
                } else if (pos > mImageViews.size() - 2) {
                    mViewPage.setCurrentItem(1, false);
                    pos = 1;
                }
                for (int i = 0; i < mPoints.getChildCount(); i++) {
                    if (pos == i + 1) {
                        mPoints.getChildAt(i).setBackgroundResource(
                                R.drawable.judge_02);
                    } else {
                        mPoints.getChildAt(i).setBackgroundResource(
                                R.drawable.judge_01);

                    }
                }
            }
        }

    }

}
